package com.primb.bench.project.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.primb.baselibs.override.SpaceItemDecoration;
import com.primb.baselibs.util.FormatUtils;
import com.primb.baselibs.util.NetworkUtils;
import com.primb.baselibs.view.AutoToolbar;
import com.primb.bench.R;
import com.primb.bench.base.SwipeBaseActivity;
import com.primb.bench.project.home.contract.LoanPromiseContract;
import com.primb.bench.project.home.entity.PromiseListEntity;
import com.primb.bench.project.home.presenter.LoanPromisePresenterImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chen on 2017/7/10.
 * 功能描述：
 */

public class LoanPromiseActivity extends SwipeBaseActivity implements LoanPromiseContract.View {
    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.tool_bar)
    AutoToolbar toolBar;
    @BindView(R.id.recycler_loan_promise)
    RecyclerView recyclerLoanPromise;
    @BindView(R.id.swipe_content)
    SwipeRefreshLayout swipeContent;
    private LoanPromiseContract.Presenter mPresenter;
    private ArrayList<PromiseListEntity.DataBean> dataList;
    private BaseQuickAdapter<PromiseListEntity.DataBean, BaseViewHolder> quickAdapter;
    private String custId;

    @Override
    protected void onBaseIntent() {
        super.onBaseIntent();
        custId = getIntent().getStringExtra("info");
    }

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_loan_promise;
    }

    @Override
    protected void onBaseBindView() {
        titleBar.setText("承诺兑现调节");
        toolBar.setNavigationOnClickListener(v -> LoanPromiseActivity.this.finish());
        swipeContent.setColorSchemeColors(ContextCompat.getColor(this, android.R.color.holo_blue_bright), ContextCompat.getColor(this, android.R.color.holo_green_light),
                ContextCompat.getColor(this, android.R.color.holo_orange_light), ContextCompat.getColor(this, android.R.color.holo_red_light));
        swipeContent.setRefreshing(true);
        configListView();
    }

    @Override
    protected void onLoadData() {
        mPresenter = new LoanPromisePresenterImpl(this, this,custId);
        mPresenter.start();
    }

    @Override
    public void showLoading() {
        showLoadingDialog("加载中");
    }

    @Override
    public void hideLoading() {
        hideLoadingDialog();
        if (swipeContent.isRefreshing()) {
            swipeContent.setEnabled(false);
        }
    }

    @Override
    public void showError(String error) {
        showToast(error);
    }

    @Override
    public void configListView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerLoanPromise.setLayoutManager(layoutManager);
        recyclerLoanPromise.setHasFixedSize(true);
        recyclerLoanPromise.setItemAnimator(new DefaultItemAnimator());
        recyclerLoanPromise.addItemDecoration(new SpaceItemDecoration(LinearLayoutManager.VERTICAL, 10));
        dataList = new ArrayList<>();
        quickAdapter = new BaseQuickAdapter<PromiseListEntity.DataBean, BaseViewHolder>(R.layout.item_loan_promise, dataList) {

            @Override
            protected void convert(BaseViewHolder helper, PromiseListEntity.DataBean item) {
                helper.setText(R.id.tv_item_deposit_avg_rate, item.getDepFinishRatio());
                helper.setText(R.id.tv_item_deposit_avg_state, item.getDepCheckResult());
                helper.setText(R.id.tv_item_deposit_balance_rate, item.getBalFinishRatio());
                helper.setText(R.id.tv_item_deposit_balance_state, item.getBalCheckResult());
                helper.setText(R.id.tv_item_basic_account, item.getBasacctCheckResult());
                helper.setText(R.id.tv_item_other_currency_account, item.getForacctCheckResult());
                helper.setText(R.id.tv_item_other_promise_state, item.getCashCheckResult());
                helper.setText(R.id.tv_item_other_approval_state, item.getCheckResult());
                helper.setText(R.id.tv_item_salary_account, item.getSalacctCheckResult());
                helper.setText(R.id.tv_item_promise_start_date, FormatUtils.formatDate(item.getPromBeginDate()));
                helper.setText(R.id.tv_item_promise_end_date, FormatUtils.formatDate(item.getPromCashDate()));
            }
        };
        quickAdapter.setOnLoadMoreListener(() -> {
            if (!NetworkUtils.isNetworkConnected(this)) {
                showError("无网络连接，请连接网络后重试");
                quickAdapter.loadMoreFail();
                return;
            }
            mPresenter.loadMoreList();
        }, recyclerLoanPromise);
        quickAdapter.isFirstOnly(true);
        View layoutEmpty = getLayoutInflater().inflate(R.layout.empty_list_view, (ViewGroup) recyclerLoanPromise.getParent(), false);
        quickAdapter.setEmptyView(layoutEmpty);
        recyclerLoanPromise.setAdapter(quickAdapter);
        swipeContent.setOnRefreshListener(() -> {
            if (!NetworkUtils.isNetworkConnected(this)) {
                showError("无网络连接，请连接网络后重试");
                swipeContent.setRefreshing(false);
                return;
            }
            mPresenter.start();
        });
    }

    @Override
    public void setDataToList(List<PromiseListEntity.DataBean> entity) {
        swipeContent.setRefreshing(false);
        dataList.clear();
        dataList.addAll(entity);
        quickAdapter.setNewData(dataList);
    }

    @Override
    public void loadMoreList(List<PromiseListEntity.DataBean> entity) {
        if (entity.size() > 0) {
            quickAdapter.addData(entity);
            quickAdapter.notifyDataSetChanged();
            quickAdapter.loadMoreComplete();
        } else {
            quickAdapter.loadMoreEnd();
        }
    }

    @Override
    public void loadMoreField() {
        quickAdapter.loadMoreFail();
    }

    @Override
    public void clearListData() {
        dataList.clear();
        quickAdapter.getData().clear();
        quickAdapter.notifyDataSetChanged();
    }
}
